package com.xiaobao.campusLostFoundAfter.controller;

import com.github.pagehelper.PageInfo;
import com.xiaobao.campusLostFoundAfter.common.Result;
import com.xiaobao.campusLostFoundAfter.pojo.Category;
import com.xiaobao.campusLostFoundAfter.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*物品种类控制器*/
@RestController
@RequestMapping("/Category")
public class CategoryController {
    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    //添加物品种类


    //获取所有物品种类
    @GetMapping("/getAll")
    public Result<List<Category>> getAllCategories() {
        List<Category> categories = categoryService.getAllCategories();
        return Result.success(categories);
    }
    /**
     * 分页查询物品分类
     */
    @GetMapping("/page")
    public Result<PageInfo<Category>> getCategoryPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String categoryName) {

        PageInfo<Category> pageResult = categoryService.getCategoryPage(pageNum, pageSize, categoryName);
        return Result.success(pageResult);
    }

    /**
     * 获取单个物品分类
     */
    @GetMapping("/{id}")
    public Result<Category> getCategoryById(@PathVariable Integer id) {
        Category category = categoryService.getCategoryById(id);
        return category != null ? Result.success(category) : Result.error("分类不存在");
    }

    /**
     * 新增物品分类
     */
    @PostMapping("/add")
    public Result<String> addCategory(@RequestBody Category category) {
        boolean success = categoryService.addCategory(category);
        return success ? Result.success("添加成功") : Result.error("添加失败");
    }

    /**
     * 更新物品分类
     */
    @PutMapping
    public Result<String> updateCategory(@RequestBody Category category) {
        boolean success = categoryService.updateCategory(category);
        return success ? Result.success("更新成功") : Result.error("更新失败");
    }

    /**
     * 删除物品分类
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteCategory(@PathVariable Integer id) {
        boolean success = categoryService.deleteCategory(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    /**
     * 批量删除物品分类
     */
    @DeleteMapping("/batch")
    public Result<String> batchDeleteCategories(@RequestBody List<Integer> ids) {
        boolean success = categoryService.batchDeleteCategories(ids);
        return success ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }
}
